2
2
.
.
4
4
.
.
2
2
I
I
n
n
t
t
e
e
r
r
f
f
a
a
c
c
e
e
s
s
I
I
n
n
f
f
o
o
Interface is collection of Methods used by end User to communicate with the Class that must implement that Interface.
This ensures that all classes that implement the same interface have the same set of functionality (interface).
Interface methods are by default
public (since they are intended to be exposed to the end user)
abstract (since prior Java 8 Interface could only have Abstract methods without implementation)
Class that implements Interface
must override Abstract methods (those without implementation)
can override Default methods (optional)
shouldn't override Static methods (forbidden)
Interfaces were introduced to avoid Multiple inheritance problem which occurs when Class can extend multiple Classes.
So in Java Class can only extend one Class but it can implement multiple interfaces.
M
M
e
e
t
t
h
h
o
o
d
d
s
s
Abstract method
can't have implementation defined inside Interface (Interface implementation is forbidden)
must be overridden in each Class that Implements Interface (Class implementation is mandatory)
Default method
must have implementation defined inside Interface (Interface implementation is mandatory)
don't need to be overridden in the Class that Implements Interface (Class implementation is optional)
Static method
must have implementation defined inside Interface (Interface implementation is mandatory)
can't be overridden in the Class that Implements Interface (Class implementation is forbidden)
can only be called in a static way (through Interface name)
A
A
b
b
s
s
t
t
r
r
a
a
c
c
t
t
c
c
l
l
a
a
s
s
s
s
e
e
s
s
v
v
s
s
i
i
n
n
t
t
e
e
r
r
f
f
a
a
c
c
e
e
s
s
After Java 8 difference is that abstract class can have constructor while interface can’t.
I
I
n
n
t
t
e
e
r
r
f
f
a
a
c
c
e
e
S
S
y
y
n
n
t
t
a
a
x
x
Class can implement multiple interfaces using keyword implements followed by the comma separated list of Interfaces.
Interface is declared by
using Keyword interface interface
followed by Interface Name InterfaceA
followed by Interface Body which can have
Methods abstract void abstractMethod() (abstract, static, default)
Syntax
interface InterfaceA { }
interface InterfaceB { }
class Person implements InterfaceA, InterfaceB { }
Syntax
//===========================================================================================================
// INTERFACE: InterfaceA
//===========================================================================================================
interface InterfaceA {
abstract void abstractMethod();
static void staticMethod () { System.out.println("staticMethod()"); }
}
//===========================================================================================================
// INTERFACE: InterfaceB
//===========================================================================================================
interface InterfaceB {
default void defaultMethod1() { System.out.println("defaultMethod1()"); }
default void defaultMethod2() { System.out.println("defaultMethod2()"); }
}
//===========================================================================================================
// CLASS: Person
//===========================================================================================================
class Person implements InterfaceA, InterfaceB {
public void abstractMethod() { System.out.println("Overriden abstractMethod()" ); }
public void defaultMethod1() { System.out.println("Overriden defaultMethod1()" ); }
}
//===========================================================================================================
// CLASS: Test
//===========================================================================================================
public class Test {
public static void main(String args[]) {
//CLASS
Person john = new Person();
john.abstractMethod();
john.defaultMethod1();
john.defaultMethod2();
//INTERFACE
InterfaceA.staticMethod();
}
}